package com.atguigu.daijia.rules.service.impl;

import com.atguigu.daijia.model.form.rules.FeeRuleRequestForm;
import com.atguigu.daijia.model.vo.rules.FeeRuleResponse;
import com.atguigu.daijia.model.vo.rules.FeeRuleResponseVo;
import com.atguigu.daijia.rules.service.FeeRuleService;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class FeeRuleServiceImpl implements FeeRuleService {
    @Autowired
    private KieContainer kieContainer;


    /**
     * 预计订单金额计算
     * 采用规则流引擎计算
     * @param calculateOrderFeeForm
     * @return
     */
    @Override
    public FeeRuleResponseVo calculateOrderFee(FeeRuleRequestForm calculateOrderFeeForm) {
        FeeRuleResponse feeRuleResponse = new FeeRuleResponse();
        FeeRuleResponseVo feeRuleResponseVo = new FeeRuleResponseVo();
        //从 kie容器中获取会话对象
        KieSession session = kieContainer.newKieSession();
        //构建事实对象
        session.insert(calculateOrderFeeForm);
        //执行相关规则
        //全局
        session.setGlobal("feeRuleResponse",feeRuleResponse);
        session.fireAllRules();

        //关闭会话
        session.dispose();

        BeanUtils.copyProperties(feeRuleResponse,feeRuleResponseVo);
        return feeRuleResponseVo;
    }
}
